package com.kika.obwiki.service.impl;

import com.kika.obwiki.entity.Content;
import com.kika.obwiki.mapper.ContentMapper;
import com.kika.obwiki.mapper.DocMapper;
import com.kika.obwiki.mapper.EbookMapper;
import com.kika.obwiki.entity.Doc;
import com.kika.obwiki.service.IContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 文档内容服务实现类
 *
 * @author cr
 * @since 2025-06-22
 */
@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements IContentService {

  @Autowired
  private DocMapper docMapper;

  @Autowired
  private EbookMapper ebookMapper;

  /**
   * 查询文档内容，并增加阅读数
   * 
   * @param id 文档ID
   * @return 文档内容
   */
  @Override
  public String findContent(Long id) {
    // 文档阅读数+1
    docMapper.increaseViewCount(id);
    // 电子书阅读数+1
    Doc doc = docMapper.selectById(id);
    if (doc != null && doc.getEbookId() != null) {
      ebookMapper.increaseViewCount(doc.getEbookId());
    }
    Content content = this.baseMapper.selectById(id);
    if (content != null) {
      return content.getContent();
    }
    return null;
  }
}
